package likou;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class _797 {
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        dfs(graph,0,new HashSet<Integer>(){{add(0);}},new ArrayList<Integer>(){{add(0);}});
        return res;
    }

    private void dfs(int[][] graph, int i, Set<Integer> set,List<Integer> list) {
        for (int j:graph[i]){
            if (set.contains(j)) continue;
            list.add(j);
            set.add(j);
            if (j==graph.length-1) res.add(new ArrayList<>(list));
            else dfs(graph,j,set,list);
            list.remove(list.size()-1);
            set.remove(j);
        }
    }
}
